/* 
 * Copyright (C) 2011 hoits.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.hoits;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Config;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class BruttoNettoActivity extends Activity {
	protected static final String TAG = "BruttoNettoActivity";

	private static final float STEUER_FULL = 1.19f;
	private static final float STEUER_HALF = 1.07f;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		final Button bruttoButton = (Button) findViewById(R.id.btnZuBrutto);
		bruttoButton.setOnClickListener(new OnClickListener() {
			public void onClick(final View view) {
				if (Config.LOGD) {
					Log.d(TAG, "msg1"); // Good
				}

				EditText edtView = (EditText) findViewById(R.id.editText1);
				edtView.setInputType(0);

				RadioButton radioFull = (RadioButton) findViewById(R.id.radio0);
				if (radioFull.isChecked()) {
					rechneBrutto(STEUER_FULL);
				} else {
					rechneBrutto(STEUER_HALF);
				}
			}
		});

		final Button nettoButton = (Button) findViewById(R.id.btnZuNetto);
		nettoButton.setOnClickListener(new OnClickListener() {
			public void onClick(final View view) {
				if (Config.LOGD) {
					Log.d(TAG, "msg1"); // Good
				}
				EditText edtView = (EditText) findViewById(R.id.editText1);
				edtView.setInputType(0);
				RadioButton radioFull = (RadioButton) findViewById(R.id.radio0);
				if (radioFull.isChecked()) {
					rechneNetto(STEUER_FULL);
				} else {
					rechneNetto(STEUER_HALF);
				}
			}
		});

		final Button clearButton = (Button) findViewById(R.id.btnClear);
		clearButton.setOnClickListener(new OnClickListener() {
			public void onClick(final View view) {
				if (Config.LOGD) {
					Log.d(TAG, "ClearButton"); // Good
				}
				EditText eingabe = (EditText) findViewById(R.id.editText1);
				eingabe.setText(null);
				TextView ausgabe = (TextView) findViewById(R.id.textView1);
				ausgabe.setText(null);
			}
		});

		SharedPreferences settings = getPreferences(0);
		TextView ausgabe = (TextView) findViewById(R.id.textView1);
		ausgabe.setText(settings.getString(getLocalClassName(), "nicht da"));
	}

	private void rechneBrutto(float faktor) {
		EditText eingabe = (EditText) findViewById(R.id.editText1);
		String s = eingabe.getText().toString();
		if (s != null && s.length() > 0) {
			double erg = Float.valueOf(s).floatValue() * faktor;
			TextView ausgabe = (TextView) findViewById(R.id.textView1);
			ausgabe.setText(String.valueOf(erg));
		}

		showAbout();
	}

	private void rechneNetto(float faktor) {
		EditText eingabe = (EditText) findViewById(R.id.editText1);
		String s = eingabe.getText().toString();
		if (s != null && s.length() > 0) {
			float wert = Float.valueOf(s).floatValue();
			if (wert != 0f) {
				double erg = wert / faktor;
				TextView ausgabe = (TextView) findViewById(R.id.textView1);
				ausgabe.setText(String.valueOf(erg));
			}
		}
	}

	/**
	 * Show an about dialog that cites data sources.
	 */
	protected void showAbout() {
		// Inflate the about message contents
		View messageView = getLayoutInflater().inflate(R.layout.about, null,
				false);

		// When linking text, force to always use default color. This works
		// around a pressed color state bug.
		TextView textView = (TextView) messageView
				.findViewById(R.id.about_credits);
		int defaultColor = textView.getTextColors().getDefaultColor();
		textView.setTextColor(defaultColor);

		Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(R.drawable.icon);
		builder.setTitle(R.string.app_name);
		builder.setView(messageView);
		builder.create();
		builder.show();
	}

	@Override
	protected void onStop() {
		super.onStop();

		// We need an Editor object to make preference changes.
		// All objects are from android.context.Context
		SharedPreferences settings = getPreferences(0);
		SharedPreferences.Editor editor = settings.edit();

		TextView ausgabe = (TextView) findViewById(R.id.textView1);
		String foo = ausgabe.getText().toString();
		editor.putString(getLocalClassName(), foo);

		// Commit the edits!
		editor.commit();
	}

}